package com.mib.snmp;

import com.mib.browser.OperationResult;
import com.mib.browser.OperationTableModel;
import org.snmp4j.smi.VariableBinding;

import javax.swing.*;
import java.util.List;

public class SnmpWalkThread extends Thread {

    private String lastOid;
    private SnmpBuilder build;
    private JButton goButton;
    private JComboBox oidComboBox;
    private OperationTableModel tableModel;

    public SnmpWalkThread(String lastOid, SnmpBuilder build, JButton goButton, JComboBox oidComboBox, OperationTableModel tableModel) {
        this.lastOid = lastOid;
        this.build = build;
        this.goButton = goButton;
        this.oidComboBox = oidComboBox;
        this.tableModel = tableModel;
    }

    @Override
    public void run() {
        while (true){
            try {
                List<VariableBinding> list = Snmp4jUtil.snmpGetNext(build.getSnmp(), build.getTarget(), lastOid);
                for(VariableBinding vb : list){
                    if(vb != null){
                        OperationResult r = new OperationResult(vb.getOid().toString(), vb.getVariable().toString());
                        tableModel.addOperationResult(r);
                        lastOid = r.getOid();
                        oidComboBox.setSelectedItem(lastOid);
                    }
                }
                Thread.sleep(1);
            } catch (Exception e) {
                break;
            }
        }
        this.goButton.setEnabled(true);
        JOptionPane.showMessageDialog(new JFrame(), " 已到达MIB视图的末尾 ", "提示", JOptionPane.WARNING_MESSAGE);
    }
}
